# -*- coding: utf-8 -*- 
'''
Created on 2012-3-5

@author: TOM
'''
from django.db.models import F
from django.http import HttpResponse
from django.utils.simplejson import dumps
from django.views.decorators.csrf import csrf_exempt
from website import utils, grids
from website.customercontrol.models import TrainingContract
from website.resourcecontrol.models import Notice, NoticeLookupLog, \
    LettersLookupLog, Letters, CourseCurrentRecord, ScheduleRecord
import datetime

#首页系统公告   
@csrf_exempt
def index_notice(request):
    CurrentUser=request.session.get('CurrentUser')
    queryset=Notice.objects.exclude(id__in=NoticeLookupLog.objects.filter(noticer__id=CurrentUser.employe.id).values('notice__id'))
#    not_in_ids = [looklog.notice.id for looklog in NoticeLookupLog.objects.filter(noticer__id=CurrentUser.employe.id)]
#    selected_objects = [letters for letters in Notice.objects.iterator() if letters.id not in not_in_ids]
    return utils.JsonResponse(grids.SimpleGrid().to_grid(None,list(queryset.values('id','title','content'))))


#首页函件提醒   
@csrf_exempt
def index_letters(request):
    CurrentUser=request.session.get('CurrentUser')
    queryset=Letters.objects.filter(receiver__id=CurrentUser.employe.id).exclude(id__in=LettersLookupLog.objects.filter(looker__id=CurrentUser.employe.id).values('letters__id'))
    return utils.JsonResponse(grids.SimpleGrid().to_grid(None,list(queryset.values('id','title','send_time'))))

#首页课时预警    
@csrf_exempt
def index_course_warning(request):
    queryset=CourseCurrentRecord.objects.exclude(course_times=F('use_course_times')).filter(course_times__lte=F('use_course_times')+5);
    return utils.JsonResponse(grids.SimpleGrid().to_grid(None,list(queryset.values('id','student__name','student__company__cn_name',
                                                                                   'student__student_manager__name','course__name',
                                                                                   'study_type','course_times','use_course_times'))))

#合同到期预警
@csrf_exempt
def index_contract_warning(request):
    queryset=TrainingContract.objects.filter(end_time__day=datetime.datetime.now().day-1)
    #queryset=TrainingContract.objects.filter()
    return utils.JsonResponse(grids.SimpleGrid().to_grid(None,list(queryset.values('id','name','student__name','student__company__cn_name','student__student_manager__name','contract_type','end_time'))))


#日程提醒
@csrf_exempt
def index_schedule(request):
    CurrentUser=request.session.get('CurrentUser')
    queryset=ScheduleRecord.objects.exclude(rem='').filter(recorder__id=CurrentUser.employe.id)
    items=[]
    for qs in queryset:
        if datetime.datetime.now() < qs.start and qs.start <= (datetime.datetime.now()+datetime.timedelta(minutes=int(qs.rem))):
            item={}
            item['id']=qs.id
            item['title']=qs.title
            item['notes']=qs.notes
            items.append(item)
    return utils.JsonResponse(grids.SimpleGrid().to_grid(None,items))


if __name__=='__main__':
    pass
#    print time.mktime(nt.timetuple())
#    print datetime.datetime.fromordinal(time.mktime(nt.timetuple()))
    #print datetime.datetime.now()+datetime.timedelta(minutes=10)